package token.mobileapp.controlactivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import token.mobileapp.R;
import token.mobileapp.TokenActivity;
import token.mobileapp.model.UserInfo;
import token.mobileapp.persistenceactivity.UserInfoPersistence;
import token.mobileapp.utils.Constants;
import token.mobileapp.utils.NetworkUtils;

import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * UserInfo: AVAVT
 * Date: 7/5/11
 * Time: 5:44 AM
 */
public class Initialization extends TokenActivity implements View.OnClickListener {
    private Button buttonNext, buttonInit;
    private EditText etPINCode, etSecretKey, etAccountCode;
    private UserInfo userInfo = new UserInfo();

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.initialization_pin);
        buttonNext = (Button) findViewById(R.id.init1_btn_next);
        buttonNext.setOnClickListener(this);
        etPINCode = (EditText) findViewById(R.id.init1_et_PIN);
    }

    public void onClick(View v) {
        if(v.getId() == R.id.init1_btn_next){
            if (etPINCode.getText().toString().compareTo("") == 0){
                displayAlert("Please enter a PIN code.");
            }
            else if(etPINCode.getText().toString().length() != 6){
                displayAlert("PIN code must be a 6-digits number.");
            }
            else{
                userInfo.setPINCode(etPINCode.getText().toString());

                setContentView(R.layout.initialization_account);
                buttonInit = (Button) findViewById(R.id.init2_btn_init);
                buttonInit.setOnClickListener(this);
                etSecretKey = (EditText) findViewById(R.id.init2_et_secretKey);
                etAccountCode = (EditText) findViewById(R.id.init2_et_accountCode);
            }
        }
        else if(v.getId() == R.id.init2_btn_init){
            if(etSecretKey.getText().toString().compareTo("") == 0){
                displayAlert("Please enter a secret key.");
            }
            else if(etSecretKey.getText().toString().length() != 6){
                displayAlert("Secret key must be a 6-digits number.");
            }
            else if(etAccountCode.getText().toString().compareTo("") == 0){
                displayAlert("Please enter your account code.");
            }
            else if(etAccountCode.getText().toString().length() != 10){
                displayAlert("Deformed account code.");
            }
            else{
                buttonInit.setClickable(false);
                userInfo.setSecretKey(etSecretKey.getText().toString());
                init();
            }
        }
    }

    private void init() {
        try {
            userInfo = NetworkUtils.initialize(userInfo, etAccountCode.getText().toString());
            if(userInfo.getSeed() == null || userInfo.getSeed().compareTo("") == 0){
                displayAlert("Internal server error. Please try again later.");
            }
            else{
                Intent intent = new Intent(this, UserInfoPersistence.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("userInfo", userInfo);
                intent.putExtras(bundle);
                startActivityForResult(intent, Constants.REQUEST_CODE_SAVE_USER);
            }
        } catch (IOException e) {
            displayAlert("A problem occurred while trying to connect to server, please make sure you have internet connection and try again.");
            buttonInit.setClickable(true);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //save success
        if (requestCode == Constants.REQUEST_CODE_SAVE_USER && resultCode == Activity.RESULT_OK) {
            Intent intent = new Intent(this, MainMenu.class);
            startActivity(intent);
            displayAlertThenFinish("Initialization completed successfully!");
        }
        //save failed
        else if (requestCode == Constants.REQUEST_CODE_SAVE_USER && resultCode == Activity.RESULT_CANCELED) {
            displayError(data);
            buttonInit.setClickable(true);
            setContentView(R.layout.initialization_pin);
            setContentView(R.layout.initialization_pin);
            buttonNext = (Button) findViewById(R.id.init1_btn_next);
            buttonNext.setOnClickListener(this);
            etPINCode = (EditText) findViewById(R.id.init1_et_PIN);
        }
    }
}
